package util;

import loader.*;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import util.comparator.ComparatorByClass;
import util.comparator.ComparatorByCorpo;
import util.comparator.ComparatorByUnitA;
import util.comparator.ComparatorByUnitB;

import data_stuct.*;

/**
 * class of util functions
 * 
 * @author Majid Mallis
 * 
 */
public class Util {

	/**
	 * sorts data by class number then by activity center's number
	 * 
	 * @param loader
	 */
	public static void sortByClass(Loader loader) {
		Entry[] dbArray = new Entry[loader.getDb().size()];
		dbArray = loader.getDb().toArray(dbArray);
		Arrays.sort(dbArray, new ComparatorByClass());
		List<Entry> l = Arrays.asList(dbArray);
		loader.setDb(new Vector<Entry>(l));
	}

	/**
	 * sorts data by corpo number then by activity center's number
	 * 
	 * @param loader
	 */
	public static void sortByCorpo(Loader loader) {
		Entry[] dbArray = new Entry[loader.getDb().size()];
		dbArray = loader.getDb().toArray(dbArray);
		Arrays.sort(dbArray, new ComparatorByCorpo());
		List<Entry> l = Arrays.asList(dbArray);
		loader.setDb(new Vector<Entry>(l));
	}

	/**
	 * sorts data by number of UnitA
	 * 
	 * @param loader
	 */
	public static void sortByUnitA(Loader loader) {
		Entry[] dbArray = new Entry[loader.getDb().size()];
		dbArray = loader.getDb().toArray(dbArray);
		Arrays.sort(dbArray, new ComparatorByUnitA());
		List<Entry> l = Arrays.asList(dbArray);
		loader.setDb(new Vector<Entry>(l));
	}

	/**
	 * sorts data by number of UnitB
	 * 
	 * @param loader
	 */
	public static void sortByUnitB(Loader loader) {
		Entry[] dbArray = new Entry[loader.getDb().size()];
		dbArray = loader.getDb().toArray(dbArray);
		Arrays.sort(dbArray, new ComparatorByUnitB());
		List<Entry> l = Arrays.asList(dbArray);
		loader.setDb(new Vector<Entry>(l));
	}

	/**
	 * 
	 * @param e
	 *            A data entry
	 * @param TypeCA
	 *            'C' for Clinique and 'S' for Soutien
	 * @return true if this entry concerns a CA Clinique, and false if not. When
	 *         e doesn't appear in the list, the methode returns false and
	 *         writes an error message in error default console.
	 */
	public static boolean isCAClinique(Entry e, Vector<Couple> TypeCA) {
		if (e.CENT_ACT[0] == '0')
			return false;
		for (Couple current : TypeCA) {
			if (new String(current.getCA()).compareTo(new String(e.CENT_ACT)) == 0) {
				return current.getType() == 'C';
			}
		}

		System.err.print("Error: Activity center number "
				+ new String(e.CENT_ACT) + " was not found!\n");
		return false;
	}

	static public void exportTableToXLS(JTable table) {
		TableModel model = table.getModel();
		JFileChooser chooser = new JFileChooser();
		int state = chooser.showSaveDialog(null);
		File file = new File(chooser.getCurrentDirectory() + File.separator
				+ chooser.getSelectedFile().getName() + ".XLS");
		if (file != null && state == JFileChooser.APPROVE_OPTION) {
			try {
				BufferedWriter bufferedWriter = new BufferedWriter(
						new FileWriter(file, true));
				PrintWriter fileWriter = new PrintWriter(bufferedWriter);

				for (int j = 0; j < model.getColumnCount(); ++j) {
					String s = model.getColumnName(j).toString();
					fileWriter.print(s);
					if (j == model.getColumnCount() - 1)
						fileWriter.print("\n");
					else
						fileWriter.print("\t");
				}
				for (int i = 0; i < model.getRowCount(); ++i) {
					for (int j = 0; j < model.getColumnCount(); ++j) {
						String s = model.getValueAt(i, j).toString();
						fileWriter.print(s);
						if (j == model.getColumnCount() - 1)
							fileWriter.print("\n");
						else
							fileWriter.print("\t");
					}
				}
				fileWriter.close();
				JOptionPane.showMessageDialog(null,
						"The table was successfuly exported!");
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"An error occured while saving table");
			}
		}
	}
}